package com.lv.suyiyong.hfr.widget

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.databinding.LayoutHomeBottomTabBinding


class HomeBottomTabWidget @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0,
//    mType: Int = 0//0 普通 1 新年主题
) : BaseHomeBottomTabWidget(context, attrs, defStyleAttr),
    View.OnClickListener {
    private var textViews: ArrayList<TextView>? = null
    private var mIvHomeTab: ImageView? = null//声明成可空类型
    private var mTotalUnread: TextView? = null//声明成可空类型
    private var mCirTotal: TextView? = null//声明成可空类型
    private var mLlHomeATHome: TextView? = null//声明成可空类型
    private var mLlHomeATCir: TextView? = null//声明成可空类型
    private var mLlHomeATCart: TextView? = null//声明成可空类型
    private var mLlHomeATMessage: TextView? = null//声明成可空类型
    private var mLlHomeATMine: TextView? = null//声明成可空类型
    private var mLlBottomTab: LinearLayout? = null//声明成可空类型

    /**
     * 初始化 设置点击事件。
     *
     */
    init { //默认第一个碎片
        val view = LayoutHomeBottomTabBinding.inflate(LayoutInflater.from(context), this, true)
        view.apply {
            mIvHomeTab = ivHomeTab
            mTotalUnread = msgTotalUnread
            mCirTotal = cirTotalUnread
            mLlHomeATHome = llHomeATHome
            mLlHomeATCir = llHomeATCircle
            mLlHomeATCart = llHomeATCart
            mLlHomeATMessage = llHomeATMessage
            mLlHomeATMine = llHomeATMine
            mLlBottomTab = llBottomTab
            textViews = arrayListOf(
                llHomeATHome,
                llHomeATCircle,
                llHomeATCart,
                llHomeATMessage,
                llHomeATMine
            )
        }

        for (textView in textViews!!) {
            textView.setOnClickListener(this)
        }
        mIvHomeTab?.setOnClickListener(this)
    }


    /**
     * 销毁，避免内存泄漏
     */
    override fun destroy() {
        super.destroy()
        if (!textViews.isNullOrEmpty()) {
            textViews?.clear()
            textViews = null
        }
    }

    /**
     * 实现按钮的点击事件
     */
    override fun onClick(v: View) {
        when (v.id) {
            R.id.iv_home_tab -> {
                listener?.OnClickTab(-1)
            }
            R.id.llHomeATHome -> {
                otherClick()
                fragmentManger(0)
            }
            R.id.llHomeATCircle -> {
                cirClick()
                fragmentManger(1)
            }
            R.id.llHomeATCart -> {
                otherClick()
                fragmentManger(2)
            }
            R.id.llHomeATMessage -> {
                otherClick()
                fragmentManger(3)
            }
            R.id.llHomeATMine -> {
                otherClick()
                fragmentManger(4)
            }
        }
    }

    private fun cirClick() {
        mLlBottomTab?.setBackgroundColor(Color.parseColor("#242424"))
        mLlHomeATHome?.setTextColor(Color.parseColor("#ffffff"))
        mLlHomeATCart?.setTextColor(Color.parseColor("#ffffff"))
        mLlHomeATMessage?.setTextColor(Color.parseColor("#ffffff"))
        mLlHomeATMine?.setTextColor(Color.parseColor("#ffffff"))
        setDrawableData(com.lv.common.R.drawable.ic_home_white_bottom, mLlHomeATHome)
        setDrawableData(com.lv.common.R.drawable.icon_findgoods_white_bottom, mLlHomeATCart)
        setDrawableData(com.lv.common.R.drawable.icon_message_white_bottom, mLlHomeATMessage)
        setDrawableData(com.lv.common.R.drawable.icon_mine_white_bottom, mLlHomeATMine)
    }

    private fun otherClick() {
        mLlBottomTab?.setBackgroundColor(Color.parseColor("#ffffff"))
        mLlHomeATHome?.setTextColor(resources.getColorStateList(R.color.home_bottom_select))
        mLlHomeATCart?.setTextColor(resources.getColorStateList(R.color.home_bottom_select))
        mLlHomeATMessage?.setTextColor(resources.getColorStateList(R.color.home_bottom_select))
        mLlHomeATMine?.setTextColor(resources.getColorStateList(R.color.home_bottom_select))
        setDrawableData(R.drawable.selector_one, mLlHomeATHome)
        setDrawableData(R.drawable.selector_two, mLlHomeATCart)
        setDrawableData(R.drawable.selector_four, mLlHomeATMessage)
        setDrawableData(R.drawable.selector_five, mLlHomeATMine)
    }

    private fun setDrawableData(intRes: Int, textView: TextView?) {
        val drawable = resources.getDrawable(intRes)
        drawable?.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
        textView?.setCompoundDrawables(null, drawable, null, null)

    }


    /**
     * fragment的切换 实现底部导航栏的切换
     *
     * @param position 序号
     */
    override fun fragmentManger(position: Int) {
        super.fragmentManger(position)
        for (j in textViews!!.indices) {
            textViews!![j].isSelected = position == j
        }
        if (position == 0) {
            mIvHomeTab?.visibility = VISIBLE
            mLlHomeATHome?.visibility = View.INVISIBLE
        } else {
            mIvHomeTab?.visibility = GONE
            mLlHomeATHome?.visibility = View.VISIBLE
        }

        listener?.OnClickTab(position)
    }

    fun setMessageCount(count: String) {
        if (count == "0") {
            mTotalUnread?.visibility = GONE
        } else {
            mTotalUnread?.visibility = VISIBLE
        }
        mTotalUnread?.text = count
    }

    fun setTabStyle(Tab: Int) {
        if (Tab == 1) {
            cirClick()
        } else {
            otherClick()
        }
    }

    fun setCartCount(count: String) {
        if (count == "0") {
            mCirTotal?.visibility = GONE
        } else {
            mCirTotal?.visibility = VISIBLE
        }
        mCirTotal?.text = count
    }

    interface TabPagerListener {
        fun OnClickTab(position: Int)
    }

    fun setOnTabPagerListener(l: TabPagerListener) {
        listener = l
    }


    var listener: TabPagerListener? = null

}